package com.databaseprotect;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Admin_MainActivity extends MainActivity {
    List<Commodity> lists=new ArrayList<>();
    ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_activity_main);
        status=true;
        //沉浸式状态栏
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            try {
                Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                field.setAccessible(true);;
                field.setInt(getWindow().getDecorView(), Color.TRANSPARENT);
            }catch(Exception e){
            }
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        mListView=findViewById(R.id.main_list);
        //获取数据库信息写入对象数组
        new Thread(new Runnable() {
            @Override
            public void run() {
                Connection conn = null;
                conn = (Connection) DBHelper.getConn();
                Statement statement;
                try {
                    statement=conn.createStatement();
                    String sql="select * from meal;";
                    ResultSet resultSet=statement.executeQuery(sql);
                    while(resultSet.next()) {
                        Commodity commodity = new Commodity();
                        commodity.name = resultSet.getString("name");
                        commodity.introduce = resultSet.getString("introduce");
                        commodity.price = resultSet.getString("price");
                        commodity.img_url=resultSet.getString("img_src");
                        commodity.mid=resultSet.getString("MID");
                        lists.add(commodity);
                    }
                    Message message = new Message();
                    message.what = 0;
                    handler.sendMessage(message);
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }).start();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView textView=(TextView)view.findViewById(R.id.mid);
                System.out.println(i);
                Intent intent=new Intent(Admin_MainActivity.this,Design.class);
                intent.putExtra("data",textView.getText().toString());
                startActivity(intent);
            }
        });
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                mListView.setAdapter(new MyAdapter());
            }
        }
    };
    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount(){
            return lists.size();
        }
        @Override
        public Object getItem(int position){
            return null;
        }
        @Override
        public long getItemId(int position){
            return 0;
        }
        @Override
        public View getView(int position, View conterview, ViewGroup parent){
            Commodity commodity=lists.get(position);
            View view=View.inflate(Admin_MainActivity.this,R.layout.admin_item,null);
            MyImageView img_src=view.findViewById(R.id.a_pay_img);
            String url="https://lemonscloud.cn/"+commodity.img_url;
            img_src.setImageURL(url);
            TextView name=view.findViewById(R.id.a_pay_name);
            name.setText(commodity.name);
            TextView introduce=view.findViewById(R.id.pay_introduce);
            introduce.setText(commodity.introduce);
            TextView price=view.findViewById(R.id.pay_price);
            price.setText("￥"+commodity.price);
            TextView mid=view.findViewById(R.id.mid);
            mid.setText(commodity.mid);
            return view;
        }
    }
    public void touser(View v){
        Intent intent=new Intent(this,User.class);
        startActivity(intent);
    }
}